﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SpringStudio.Common;
using SpringStudio.DAL;
using Sdepci.InfoCode.BLL.Authority;

namespace Sdepci.InfoCode.Web.Authority
{
    public partial class FunctionEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindData();
            }
        }

        private void BindData()
        {
            if (this.Request.QueryString["FuncGuid"] != null && this.Request.QueryString["FuncGuid"].Trim().Length > 0)
            {
                this.lblTitle.Text += "[修改]";

                Function function = new Function(this.Request.QueryString["FuncGuid"].Trim());
                this.FuncDesc.Text = function.FuncDesc;
                this.FuncUrl.Text = function.FuncUrl;
                this.FuncIcon.Text = function.FuncIcon;
                this.imgFuncIcon.ImageUrl = "../Images/MenuIcon/" + function.FuncIcon;
                this.Seq.Text = function.Seq.ToString();
            }
            else
            {
                this.lblTitle.Text += "[新增]";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.Request.QueryString["FuncGuid"] != null && this.Request.QueryString["FuncGuid"].Trim().Length > 0)
            {
                bool result = new FunctionManager().Update(this.Request.QueryString["FuncGuid"].Trim(), this.FuncDesc.Text.Trim(), this.FuncUrl.Text.Trim(), this.FuncIcon.Text.Trim(), int.Parse(this.Seq.Text.Trim()));
                string msg = result ? "菜单修改成功！" : "菜单修改失败！";

                Function function = new Function(this.Request.QueryString["FuncGuid"].Trim());

                if (!"0".Equals(function.ParentFuncGuid))
                {
                    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script language=\"javascript\">alert(\"" + msg + "\");document.location.href='FunctionList.aspx?ParentFuncGuid=" + function.ParentFuncGuid + "';</script>");
                }
                else
                {
                    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script language=\"javascript\">alert(\"" + msg + "\");document.location.href='FunctionList.aspx';</script>");
                }
            }
            else if (this.Request.QueryString["ParentFuncGuid"] != null && this.Request.QueryString["ParentFuncGuid"].Trim().Length > 0)
            {
                bool result = new FunctionManager().Add(this.Request.QueryString["ParentFuncGuid"].Trim(), this.FuncDesc.Text.Trim(), this.FuncUrl.Text.Trim(), this.FuncIcon.Text.Trim(), int.Parse(this.Seq.Text.Trim()));
                string msg = result ? "菜单新增成功！" : "菜单新增失败！";
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script language=\"javascript\">alert(\"" + msg + "\");document.location.href='FunctionList.aspx?ParentFuncGuid=" + this.Request.QueryString["ParentFuncGuid"].Trim() + "';</script>");
            }
            else
            {
                bool result = new FunctionManager().Add("0", this.FuncDesc.Text.Trim(), this.FuncUrl.Text.Trim(), this.FuncIcon.Text.Trim(), int.Parse(this.Seq.Text.Trim()));
                string msg = result ? "菜单新增成功！" : "菜单新增失败！";
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script language=\"javascript\">alert(\"" + msg + "\");document.location.href='FunctionList.aspx';</script>");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (this.Request.QueryString["FuncGuid"] != null && this.Request.QueryString["FuncGuid"].Trim().Length > 0)
            {
                Function function = new Function(this.Request.QueryString["FuncGuid"].Trim());
                if (!"0".Equals(function.ParentFuncGuid))
                {
                    this.Response.Redirect("FunctionList.aspx?ParentFuncGuid=" + function.ParentFuncGuid);
                }
                else
                {
                    this.Response.Redirect("FunctionList.aspx");
                }
            }
            else if (this.Request.QueryString["ParentFuncGuid"] != null && this.Request.QueryString["ParentFuncGuid"].Trim().Length > 0)
            {
                this.Response.Redirect("FunctionList.aspx?ParentFuncGuid=" + this.Request.QueryString["ParentFuncGuid"].Trim());
            }
            else
            {
                this.Response.Redirect("FunctionList.aspx");
            }
        }
    }
}
